package com.dk.controller;

import com.dk.data.Result;
import com.dk.em.ConstantEnum;
import com.dk.pojo.TContent;
import com.dk.service.ITContentService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 广告图片表 前端控制器
 * </p>
 *
 * @author author
 * @since 2021-10-29
 */
@RestController
@RequestMapping("/t-content")
@Api(value = "广告图片管理")
public class TContentController {

    @Autowired
    private ITContentService contentService;

    //查询id
    @GetMapping("/{id}")
    @ApiOperation(value = "广告查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "广告图片id",required = true,paramType = "path")
    })
    public Result findById(@PathVariable("id") Long id){

        TContent content = contentService.getById(id);
        return Result.bulid(ConstantEnum.SUCCESS).setResult(content);
    }

    //增加
    @ApiOperation(value = "广告图片增加接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title",value = "标题",required = true),
            @ApiImplicitParam(name = "pic",value = "图片",required = true),
            @ApiImplicitParam(name = "startTime",value = "开始时间",required = true),
            @ApiImplicitParam(name = "endTime",value = "结束时间",required = true),
            @ApiImplicitParam(name = "categoryId",value = "广告类型",required = true),
            @ApiImplicitParam(name = "sort",value = "排序",required = true)
    })
    @PostMapping(value = "/insert")
    public Result insert(TContent content){
       /* String upload = upload(myFile);
        content.setPic(upload);*/
        contentService.save(content);
        return Result.bulid(ConstantEnum.SUCCESS);
    }

    //修改
    @PutMapping(value = "/{id}/{title}/{pic}")
    @ApiOperation(value = "广告图片修改接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "广告图片id",required = true,paramType = "path"),
            @ApiImplicitParam(name = "title",value = "标题",required = true,paramType = "path")
    })
    public Result update(@PathVariable("id") Long id,@PathVariable("title") String title,@PathVariable("pic") String pic){
        TContent content1 = contentService.getById(id);
        content1.setTitle(title);
        //String s = upload(myFile);
        content1.setPic(pic);
        contentService.updateById(content1);
        return Result.bulid(ConstantEnum.SUCCESS);
    }

    //删除
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "广告图片批量删除接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids",value = "广告图片id",required = true,paramType = "path")
    })
    public Result delete(@PathVariable("ids") Long[] ids){
        contentService.removeByIds(Arrays.asList(ids));
        return Result.bulid(ConstantEnum.SUCCESS);
    }

    //全部查询
    @RequestMapping(value = "findAll",method = RequestMethod.GET)
    @ApiOperation(value = "广告图片全部查询接口")
    public Result findAll(){
        List<TContent> list = contentService.list();
        return Result.bulid(ConstantEnum.SUCCESS).setResult(list);
    }

    /*public String upload(@RequestParam("image") MultipartFile image){
        // yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1（杭州）为例，
        // Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
        String http = "https://";
        String endpoint = "oss-cn-beijing.aliyuncs.com";
        String buketName = "dongdongshop-2021";
        // 阿里云账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维，请登录RAM控制台创建RAM用户。
        String accessKeyId = "LTAI5tG3iqSJKw7zGH6hGZz7";
        String accessKeySecret = "KqyTjhd8Tfw1ugEAJlt9qjaOezIPXr";

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        // 填写字符串。
        //String content = "Hello OSS";
        String filename = null;
        try {
            InputStream inputStream = image.getInputStream();
            filename = "test/" + image.getOriginalFilename();


            // 创建PutObjectRequest对象。
            // 依次填写Bucket名称（例如examplebucket）和Object完整路径（例如exampledir/exampleobject.txt）。Object完整路径中不能包含Bucket名称。
            PutObjectRequest putObjectRequest = new PutObjectRequest(buketName, filename, inputStream);

            // 如果需要上传时设置存储类型和访问权限，请参考以下示例代码。
            // ObjectMetadata metadata = new ObjectMetadata();
            // metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());
            // metadata.setObjectAcl(CannedAccessControlList.Private);
            // putObjectRequest.setMetadata(metadata);

            // 上传字符串。
            ossClient.putObject(putObjectRequest);

            // 关闭OSSClient。
            ossClient.shutdown();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return http + buketName + "." + endpoint + "/" + filename;
    }*/
}
